package com.example.planstudio;

import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.RotateAnimation;

import java.util.Random;
public class MyView extends View implements View.OnClickListener {
    private String[] lunch = new String[]{"辣 椒 炒 肉","老 坛 酸 菜 面","  麻 辣 烫", "兰 州 拉 面", "拌 面 扁 肉", "日 本 寿 司", "  蛋 炒 饭", "南 昌 米 粉"};
//    private String[] snack = new String[]{"辣 椒 炒 肉","老 坛 酸 菜 面","  麻 辣 烫", "兰 州 拉 面", "拌 面 扁 肉", "日 本 寿 司", "  蛋 炒 饭", "南 昌 米 粉"};
//    private String[] breakfast = new String[]{"辣 椒 炒 肉","老 坛 酸 菜 面","  麻 辣 烫", "兰 州 拉 面", "拌 面 扁 肉", "日 本 寿 司", "  蛋 炒 饭", "南 昌 米 粉"};

    private String mStr = "开始";

//    String msg = intent.getStringExtra("msg");
    private String[] contents = lunch;
    public int[] colors = new int[]{Color.parseColor("#8EE5EE"), Color.parseColor("#48FF00"), Color.parseColor("#FFD39B"), Color.parseColor("#FF8247"), Color.parseColor("#FF34B3"), Color.parseColor("#F0E68C"),Color.parseColor("#8A2BE2"),Color.parseColor("#00BFFF")};
    private int mWidth;
    private Paint mPaint;
    private Context mContext;
    private float startjs = 0f;
    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mContext = context;
        setOnClickListener(this);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setColor(Color.YELLOW);
        mPaint.setStyle(Paint.Style.STROKE);
        //设置边缘锯齿
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(3);
        canvas.drawCircle(mWidth / 2, mWidth / 2, mWidth / 2, mPaint);
        RectF rectF = new RectF(0, 0, mWidth, mWidth);
        mPaint.setStyle(Paint.Style.FILL);
        for (int i = 0; i < colors.length; i++) {
            //给扇形填充颜色
            mPaint.setColor(colors[i]);
            int startjd = i * 45;
            //参数的意思  画扇形    开始的角度    结束的角度     是否有中心    画笔
            canvas.drawArc(rectF, startjd, 45, true, mPaint);
        }
        //字体颜色
        mPaint.setColor(Color.WHITE);
        //字体大小
        mPaint.setTextSize(40);
        //进行循环
        int w = getWidth();
        int h = getHeight();
        Path path = new Path();
        path.moveTo(-110,250);
        path.lineTo(w/2, h / 2);
        path.close();
        for (int i = 0; i < contents.length; i++) {
            //设定文字的路径
            canvas.save();
            canvas.rotate(45 * i, w / 2, h / 2);
            canvas.drawTextOnPath(contents[i], path, 190, 0, mPaint);
        }
        mPaint.setColor(Color.RED);
        canvas.drawCircle(mWidth / 2, mWidth / 2, 50, mPaint);
        mPaint.setColor(Color.BLUE);
        mPaint.setTextSize(30);
        //要得到我们写的字的高和宽
        Rect rect = new Rect();
        mPaint.getTextBounds(mStr, 0, mStr.length(), rect);
        int width = rect.width();
        int height = rect.height();
        canvas.drawText(mStr, mWidth / 2 - width / 2, mWidth / 2 + height / 2, mPaint);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(900, 900);
        //得到测量过后的高和宽
        mWidth = getMeasuredWidth();
    }

    @Override
    public void onClick(View v) {
        //随机数
        Random random = new Random();
        int js = random.nextInt(3240);
        int last = random.nextInt(360) + 360;
        //旋转动画
        RotateAnimation rotateAnimation = new RotateAnimation(startjs, js + last, mWidth / 2, mWidth / 2);
        //旋转时间
        rotateAnimation.setDuration(6000);
        //保留最后执行完的位置
        rotateAnimation.setFillAfter(true);
        //启动动画
        startAnimation(rotateAnimation);
        startjs = (js + last) % 360;
    }
}